Page({
    data: {
      userRole: '',
      classes: [],
      joinedClasses: []
    },
  
    onLoad: function() {
      this.setData({
        userRole: wx.getStorageSync('userRole')
      });
      this.loadClasses();
    },
  
    onShow: function() {
      this.loadClasses();
    },
  
    loadClasses: function() {
      const that = this;
      if (this.data.userRole === 'teacher') {
        // 加载教师创建的班级
        wx.request({
          url: `${getApp().globalData.baseUrl}/api/classes/teacher`,
          method: 'GET',
          header: {
            'Authorization': `Bearer ${wx.getStorageSync('token')}`
          },
          success(res) {
            if (res.data.success) {
              that.setData({
                classes: res.data.classes
              });
            }
          }
        });
      } else {
        // 加载学生加入的班级
        wx.request({
          url: `${getApp().globalData.baseUrl}/api/classes/student`,
          method: 'GET',
          header: {
            'Authorization': `Bearer ${wx.getStorageSync('token')}`
          },
          success(res) {
            if (res.data.success) {
              that.setData({
                joinedClasses: res.data.classes
              });
            }
          }
        });
      }
    },
  
    showCreateClass: function() {
      wx.showModal({
        title: '创建班级',
        editable: true,
        placeholderText: '请输入班级名称',
        success: (res) => {
          if (res.confirm && res.content) {
            this.createClass(res.content);
          }
        }
      });
    },
  
    createClass: function(name) {
      const that = this;
      wx.request({
        url: `${getApp().globalData.baseUrl}/api/classes`,
        method: 'POST',
        header: {
          'Authorization': `Bearer ${wx.getStorageSync('token')}`
        },
        data: { name },
        success(res) {
          if (res.data.success) {
            wx.showToast({
              title: '创建成功',
              icon: 'success'
            });
            that.loadClasses();
          } else {
            wx.showToast({
              title: res.data.message,
              icon: 'none'
            });
          }
        }
      });
    },
  
    showJoinClass: function() {
      wx.showModal({
        title: '加入班级',
        editable: true,
        placeholderText: '请输入班级码',
        success: (res) => {
          if (res.confirm && res.content) {
            this.joinClass(res.content);
          }
        }
      });
    },
  
    joinClass: function(code) {
      const that = this;
      wx.request({
        url: `${getApp().globalData.baseUrl}/api/classes/join`,
        method: 'POST',
        header: {
          'Authorization': `Bearer ${wx.getStorageSync('token')}`
        },
        data: { code },
        success(res) {
          if (res.data.success) {
            wx.showToast({
              title: '加入成功',
              icon: 'success'
            });
            that.loadClasses();
          } else {
            wx.showToast({
              title: res.data.message,
              icon: 'none'
            });
          }
        }
      });
    }
  });